package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.OrderSettingVo;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.ConvertUtils;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
@CrossOrigin
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    //查看页面
    @GetMapping("/getOrderSettingsByMonth")
    public Result findpage(@RequestParam String date){


        try {
            List<OrderSettingVo>list =orderSettingService.findpage(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //修改预约人数
    @PutMapping("/updateOrderSettingNumber")
    public Result updateNumber(@RequestBody Map map){

        try {
            String orderDate = (String) map.get("orderDate");
            int number = Integer.parseInt(map.get("number").toString());
            orderSettingService.updateNumber(orderDate,number);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @PostMapping("/upload")
    public Result postData(@RequestParam("excelFile") MultipartFile excelFile){

        try {
            POIUtils.checkFile(excelFile);
            List<String[]> strings = POIUtils.readExcel(excelFile);

            orderSettingService.postData(strings);

            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

}
